﻿/******************************************************/
// BOLWarehouse 事業運営クラス  
/******************************************************/

using System;
using System.Data;
using System.Data.SqlClient;
using log4net;
using MAA.Data;
using MAA.Entity;

namespace MAA.Business
{
    /// <summary>
    /// Do bussiness logic refer to manage warehouse
    /// </summary>
    public class BOLWarehouse
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// View information of all rice in current user's warehouse 
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="tblWhRice"></param>
        /// <param name="tblRcTypeToAdd"></param>
        /// <param name="tblRcTypeToCreate"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewWhRice(string lang,long userID, out DataTable tblWhRice, out string message)
        {
            tblWhRice = new DataTable();
            message = "";
            try
            {
                log.Info("view wh rice");
                DALWarehouse dalWh = new DALWarehouse();
                // Get rice in warehouse
                tblWhRice = dalWh.getWhRice(lang, userID);

                DataColumn column;
                column = new DataColumn();
                column.ColumnName = "MaxPrice";
                column.DataType = typeof(string);
                tblWhRice.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "MinPrice";
                column.DataType = typeof(string);
                tblWhRice.Columns.Add(column);

                int currentPrice;
                short percentPrice;
                int minPrice;
                int maxPrice;

                for (int i = 0; i < tblWhRice.Rows.Count; i++)
                {
                    currentPrice = Convert.ToInt16(tblWhRice.Rows[i]["CurrentPrice"]);
                    percentPrice = Convert.ToInt16(tblWhRice.Rows[i]["PercentPrice"]);
                    minPrice = currentPrice - (currentPrice * percentPrice / 100);
                    maxPrice = currentPrice + (currentPrice * percentPrice / 100);
                    tblWhRice.Rows[i]["MaxPrice"] = maxPrice.ToString();
                    tblWhRice.Rows[i]["MinPrice"] = minPrice.ToString();
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// View name of all rice types that user can add to warehouse
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="tblTypeAdd"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewRiceTypeToAdd(string lang, long userID, out DataTable tblTypeAdd, out string message)
        {
            tblTypeAdd = new DataTable();
            message = "";
            try
            {
                log.Info("view rice type to add");
                DALRice dalRice = new DALRice();
                // Get rice type to add
                // Type to add not in warehose
                tblTypeAdd = dalRice.getRiceTypeToAdd(lang, userID);
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// View name of all rice types that user can use to create selling post
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="tblTypeCreate"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewRiceTypeToCreate(string lang ,long userID, out DataTable tblTypeCreate, out string message)
        {
            tblTypeCreate = new DataTable();
            message = "";
            try
            {
                log.Info("view rice type to create");
                DALRice dalRice = new DALRice();
                // Get rice type to create new post
                // Type to creare  must be in warehouse
                tblTypeCreate = dalRice.getRiceTypeToCreate(lang, userID);
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// Add a rice to current user's warehouse
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="whRice"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool addWhRice(long userID, int riceID, long amount, out string message)
        {
            message = "";
            try
            {
                log.Info("add wh rice");
                DALWarehouse dalWh = new DALWarehouse();
                // Add a rice type and amount of it into warehouse
                dalWh.insertWhRice(userID, riceID, amount);
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// Edit a rice in current user's warehouse
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="whRice"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool editWhRice(long userID, int riceID, long amount, out string message)
        {
            message = "";
            try
            {
                log.Info("edit wh rice");
                DALWarehouse dalWh = new DALWarehouse();
                // Update amount of a rice type in warehouse
                dalWh.updateWhRice(userID, riceID, amount);
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// Delete a rice in current user's warehouse
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="riceID"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool deleteWhRice(long userID, int riceID, out string message)
        {
            message = "";
            try
            {
                log.Info("delete wh rice");
                DALPost dalPost = new DALPost();
                DALWarehouse dalWh = new DALWarehouse();
                // Lay so luong bai post 
                DataTable tblNumPost = dalPost.getPost(userID, riceID);
                int numPost = 0;
                if (tblNumPost != null && tblNumPost.Rows.Count > 0)
                {
                    var numRow = tblNumPost.Rows.Count;
                    for (int i = 0; i < numRow; i++)
                    {
                        var row = tblNumPost.Rows[i];
                        numPost = Convert.ToInt32(row["postNum"]);
                    }
                }
                // Neu van con post chứa loại gạo này
                if (numPost > 0)
                {
                    message = Common.MESSAGE_ERROR_STILL_HAVE_POST;
                    return false;
                }
                else
                {
                    // Delete ricetype in warehouse
                    dalWh.deleteWhRice(userID, riceID);
                    return true;
                }
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }
    }
}